package com.qzsoft.zhjd.server.modules.cement.handler;

import java.math.BigDecimal;

import com.qzsoft.zhjd.server.common.utils.NumberUtils;
import com.qzsoft.zhjd.server.modules.cement.model.CementCollectDataModel;

/**
 * @ClassName CementCollectDataHandler
 * @Description 水稳采集数据处理
 * @author linhuawei
 * @since 2022-09-26
 */
public class CementCollectDataHandler {

    /**
     * @Title handler
     * @Description 处理采集数据
     * @param originalData 采集到的数据
     * @param handlerTheo  是否要处理理论值
     */
    public static void handler(CementCollectDataModel originalData, boolean handlerTheo) {
        // 实际重量保留两位小数
        BigDecimal gravel1Act = NumberUtils.roundHalfUp(originalData.getGravel1Act(), 2);// 集料1实际用量
        BigDecimal gravel2Act = NumberUtils.roundHalfUp(originalData.getGravel2Act(), 2);// 集料2实际用量
        BigDecimal gravel3Act = NumberUtils.roundHalfUp(originalData.getGravel3Act(), 2);// 集料3实际用量
        BigDecimal gravel4Act = NumberUtils.roundHalfUp(originalData.getGravel4Act(), 2);// 集料4实际用量
        BigDecimal gravel5Act = NumberUtils.roundHalfUp(originalData.getGravel5Act(), 2);// 集料5实际用量
        BigDecimal gravel6Act = NumberUtils.roundHalfUp(originalData.getGravel6Act(), 2);// 集料6实际用量
        BigDecimal powder1Act = NumberUtils.roundHalfUp(originalData.getPowder1Act(), 2);// 水泥1实际用量
        BigDecimal powder2Act = NumberUtils.roundHalfUp(originalData.getPowder2Act(), 2);// 水泥2实际用量
        BigDecimal waterAct = NumberUtils.roundHalfUp(originalData.getWaterAct(), 2);// 水实际用量
        BigDecimal batchWeight = NumberUtils.sum(gravel1Act, gravel2Act, gravel3Act, gravel4Act, gravel5Act, gravel6Act,
                powder1Act, powder2Act, waterAct);// 批次重量

        // 理论重量保留两位小数
        BigDecimal gravel1Theo = NumberUtils.roundHalfUp(originalData.getGravel1Theo(), 2);// 集料1理论用量
        BigDecimal gravel2Theo = NumberUtils.roundHalfUp(originalData.getGravel2Theo(), 2);// 集料2理论用量
        BigDecimal gravel3Theo = NumberUtils.roundHalfUp(originalData.getGravel3Theo(), 2);// 集料3理论用量
        BigDecimal gravel4Theo = NumberUtils.roundHalfUp(originalData.getGravel4Theo(), 2);// 集料4理论用量
        BigDecimal gravel5Theo = NumberUtils.roundHalfUp(originalData.getGravel5Theo(), 2);// 集料5理论用量
        BigDecimal gravel6Theo = NumberUtils.roundHalfUp(originalData.getGravel6Theo(), 2);// 集料6理论用量
        BigDecimal powder1Theo = NumberUtils.roundHalfUp(originalData.getPowder1Theo(), 2);// 水泥1理论用量
        BigDecimal powder2Theo = NumberUtils.roundHalfUp(originalData.getPowder2Theo(), 2);// 水泥2理论用量
        BigDecimal waterTheo = NumberUtils.roundHalfUp(originalData.getWaterTheo(), 2);// 水理论用量

        // 理论占比保留一位小数
        BigDecimal gravel1RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel1RatioTheo(), 1);// 集料1理论用量
        BigDecimal gravel2RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel2RatioTheo(), 1);// 集料2理论用量
        BigDecimal gravel3RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel3RatioTheo(), 1);// 集料3理论用量
        BigDecimal gravel4RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel4RatioTheo(), 1);// 集料4理论用量
        BigDecimal gravel5RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel5RatioTheo(), 1);// 集料5理论用量
        BigDecimal gravel6RatioTheo = NumberUtils.roundHalfUp(originalData.getGravel6RatioTheo(), 1);// 集料6理论用量
        BigDecimal powder1RatioTheo = NumberUtils.roundHalfUp(originalData.getPowder1RatioTheo(), 1);// 水泥1理论用量
        BigDecimal powder2RatioTheo = NumberUtils.roundHalfUp(originalData.getPowder2RatioTheo(), 1);// 水泥2理论用量
        BigDecimal waterRatioTheo = NumberUtils.roundHalfUp(originalData.getWaterRatioTheo(), 1);// 水理论用量

        // 判断是否需要处理理论值
        if (handlerTheo) {
            // 判断理论重量是否存在
            BigDecimal totalTheo = NumberUtils.sum(gravel1Theo, gravel2Theo, gravel3Theo, gravel4Theo, gravel5Theo,
                    gravel6Theo, powder1Theo, powder2Theo, waterTheo);// 理论总量
            boolean hasTheo = null != totalTheo && -1 == BigDecimal.ZERO.compareTo(totalTheo);

            // 判读理论占比是否存在
            BigDecimal totalRatioTheo = NumberUtils.sum(gravel1RatioTheo, gravel2RatioTheo, gravel3RatioTheo,
                    gravel4RatioTheo, gravel5RatioTheo, gravel6RatioTheo, powder1RatioTheo, powder2RatioTheo,
                    waterRatioTheo);// 理论占比总量
            boolean hasRatioTheo = null != totalRatioTheo && -1 == BigDecimal.ZERO.compareTo(totalRatioTheo);

            // 如果存在重量，不存在占比，计算占比
            // 如果存在占比，不存在重量，计算重量
            if (hasTheo && !hasRatioTheo) {
                gravel1RatioTheo = NumberUtils.countRatio(totalTheo, gravel1Theo, 1);
                gravel2RatioTheo = NumberUtils.countRatio(totalTheo, gravel2Theo, 1);
                gravel3RatioTheo = NumberUtils.countRatio(totalTheo, gravel3Theo, 1);
                gravel4RatioTheo = NumberUtils.countRatio(totalTheo, gravel4Theo, 1);
                gravel5RatioTheo = NumberUtils.countRatio(totalTheo, gravel5Theo, 1);
                gravel6RatioTheo = NumberUtils.countRatio(totalTheo, gravel6Theo, 1);
                powder1RatioTheo = NumberUtils.countRatio(totalTheo, powder1Theo, 1);
                powder2RatioTheo = NumberUtils.countRatio(totalTheo, powder2Theo, 1);
                waterRatioTheo = NumberUtils.countRatio(totalTheo, waterTheo, 1);
            } else if (!hasTheo && hasRatioTheo) {
                totalTheo = batchWeight;

                gravel1Theo = NumberUtils.countValue(totalTheo, gravel1RatioTheo, 2);
                gravel2Theo = NumberUtils.countValue(totalTheo, gravel2RatioTheo, 2);
                gravel3Theo = NumberUtils.countValue(totalTheo, gravel3RatioTheo, 2);
                gravel4Theo = NumberUtils.countValue(totalTheo, gravel4RatioTheo, 2);
                gravel5Theo = NumberUtils.countValue(totalTheo, gravel5RatioTheo, 2);
                gravel6Theo = NumberUtils.countValue(totalTheo, gravel6RatioTheo, 2);
                powder1Theo = NumberUtils.countValue(totalTheo, powder1RatioTheo, 2);
                powder2Theo = NumberUtils.countValue(totalTheo, powder2RatioTheo, 2);
                waterTheo = NumberUtils.countValue(totalTheo, waterRatioTheo, 2);
            }
        }

        // 填充数据
        originalData.setGravel1Act(gravel1Act);
        originalData.setGravel2Act(gravel2Act);
        originalData.setGravel3Act(gravel3Act);
        originalData.setGravel4Act(gravel4Act);
        originalData.setGravel5Act(gravel5Act);
        originalData.setGravel6Act(gravel6Act);
        originalData.setPowder1Act(powder1Act);
        originalData.setPowder2Act(powder2Act);
        originalData.setWaterAct(waterAct);
        originalData.setBatchWeight(batchWeight);

        originalData.setGravel1Theo(gravel1Theo);
        originalData.setGravel2Theo(gravel2Theo);
        originalData.setGravel3Theo(gravel3Theo);
        originalData.setGravel4Theo(gravel4Theo);
        originalData.setGravel5Theo(gravel5Theo);
        originalData.setGravel6Theo(gravel6Theo);
        originalData.setPowder1Theo(powder1Theo);
        originalData.setPowder2Theo(powder2Theo);
        originalData.setWaterTheo(waterTheo);

        originalData.setGravel1RatioTheo(gravel1RatioTheo);
        originalData.setGravel2RatioTheo(gravel2RatioTheo);
        originalData.setGravel3RatioTheo(gravel3RatioTheo);
        originalData.setGravel4RatioTheo(gravel4RatioTheo);
        originalData.setGravel5RatioTheo(gravel5RatioTheo);
        originalData.setGravel6RatioTheo(gravel6RatioTheo);
        originalData.setPowder1RatioTheo(powder1RatioTheo);
        originalData.setPowder2RatioTheo(powder2RatioTheo);
        originalData.setWaterRatioTheo(waterRatioTheo);
    }

}
